﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using enqueteJwVd.DAL;
using enqueteJwVd.Services;
using enqueteJwVd.ViewModels;

namespace enqueteJwVd.Controllers
{
     [Authorize(Roles = "e-admin")]
    public class RolesController : Controller
    {

        UnitOfWork unitOfWork = new UnitOfWork();
        IRoleService roleService;
        //private VDLDbEntities db = new VDLDbEntities();


        public RolesController(IRoleService roleService)
        {
            this.roleService = roleService;
        }

        public RolesController()
        {
            
        }

        //
        // GET: /Roles/

        public ViewResult Index()
        {
            var roles = roleService.GetAllRoles();
            var viewModel = new RoleIndexViewModel() { Roles = roles };
            return View(viewModel);
        }

        //
        // GET: /Roles/Details/5

        public ViewResult Details(int? id)
        {
            Role role = unitOfWork.RoleRepository.GetById(id);
            return View(role);
        }

        //
        // GET: /Roles/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Roles/Create

        [HttpPost]
        public ActionResult Create([Bind(Exclude = "Id")] Role role)
        {
            if (ModelState.IsValid)
            {
                unitOfWork.RoleRepository.Add(role);
                unitOfWork.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(role);
        }
        
        //
        // GET: /Roles/Edit/5
 
        public ActionResult Edit(int? id)
        {
            Role role = unitOfWork.RoleRepository.GetById(id);
            return View(role);
        }

        //
        // POST: /Roles/Edit/5

        [HttpPost]
        public ActionResult Edit(Role role)
        {
            if (ModelState.IsValid)
            {
                unitOfWork.RoleRepository.Update(role);
                unitOfWork.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(role);
        }

        //
        // GET: /Roles/Delete/5
 
        public ActionResult Delete(int? id)
        {
            Role role = unitOfWork.RoleRepository.GetById(id);
            return View(role);
        }

        //
        // POST: /Roles/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int? id)
        {
            Role role = unitOfWork.RoleRepository.GetById(id);
            unitOfWork.RoleRepository.Delete(role);
            unitOfWork.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            unitOfWork.Dispose();
            base.Dispose(disposing);
        }
    }
}